package guowei.gulistorage.storage.service.impl;

import guowei.common.constant.PurchaseDetailStatus;
import guowei.common.constant.PurchaseStatus;
import guowei.gulistorage.storage.entity.PurchaseDetailEntity;
import guowei.gulistorage.storage.entity.UserEntity;
import guowei.gulistorage.storage.service.PurchaseDetailService;
import guowei.gulistorage.storage.service.UserService;
import guowei.gulistorage.storage.vo.MergeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import guowei.common.utils.PageUtils;
import guowei.common.utils.Query;

import guowei.gulistorage.storage.dao.PurchaseDao;
import guowei.gulistorage.storage.entity.PurchaseEntity;
import guowei.gulistorage.storage.service.PurchaseService;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    private UserService roleService;

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getUserList(Map<String, Object> params) {
        IPage<UserEntity> page = roleService.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>());

        return new PageUtils(page);
    }

    @Override
    public PageUtils getUnreceiveList(Map<String,Object> params) {

        QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("status", PurchaseStatus.NEW_TYPE.getType()).or().like("status",PurchaseStatus.FENPEI_TYPE.getType());

        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                wrapper);

        return new PageUtils(page);
    }

    /**
     * 合并菜品单 ，并更更新时间
     * @param mergeVo
     */
    @Override
    public void mergePurchase(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();
        List<Long> items = mergeVo.getItems();
        if (purchaseId == null) {
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(0);
            purchaseEntity.setPriority(0);
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();
        }
        QueryWrapper<PurchaseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",purchaseId);
        PurchaseEntity purchaseEntity = this.getOne(queryWrapper);
        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> list = items.stream().map(item -> {
                QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<PurchaseDetailEntity>().eq("id", item);
                PurchaseDetailEntity detail = purchaseDetailService.getOne(wrapper);
                detail.setPurchaseId(finalPurchaseId);
                detail.setId(item);
                detail.setStatus(PurchaseDetailStatus.FENPEI_TYPE.getType());
                return detail;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(list);
            purchaseEntity.setUpdateTime(new Date());
        }
    }